<?xml version="1.0" encoding="utf-8"?>

<!--
Guarana Release

Call task called 'deploy' to build a full release.
-->

<project name="Guarana" default="all" basedir=".">

	<property file="ant.properties"/>

	<property file="components"/>
	<property name="components" value="all"/>

	<property file="debug"/>
	<property name="debug" value="off"/>

	<property file="resolution"/>
	<property name="resolution" value="360x640"/>

	<property file="theme"/>
	<property name="theme" value="default"/>
	
	<property file="demofolder"/>
	<property name="demofolder" value="S60"/>
	
	<property file="plist.component"/>
	<property name="plist.component" value="rating"/>

	<echo message="Using theme: ${theme}"/>
	
	<property name="release.filename" value="Guarana"/>
	<property name="concatenated-themeroller" value="Themeroller"/>
	<property name="concatenated" value="Guarana"/>
	<property name="concatenated-lib" value="jquery"/>

	<property name="build.dir" value="tools"/>
	<property name="dist.dir" value="Guarana"/>
	<property name="base.dir" value="../"/>
	<property name="src.dir" value="${base.dir}/src/"/>
	<property name="lib.dir" value="${base.dir}/lib/"/>
	<property name="nokia.theme.dir" value="/themes/nokia/"/>
	<property name="themeroller.theme.dir" value="/themes/themeroller/${theme}-theme"/>

	<property name="lib.files" value="jquery.js, jquery-ui.js, jquery.scrollTo.js"/>
	<property name="core.files" value="defaults.js, core.js"/>
	<property description="YUI Compressor" name="yui-jar" value="${build.dir}/yuicompressor-2.4.2.jar"/>
	<property description="Closure Compiler" name="closure-compiler-jar" value="${build.dir}/compiler.jar"/>
	
	<property file="minifier"/>
	<property name="minifier" value="yui"/>

	<taskdef resource="net/sf/antcontrib/antcontrib.properties">
		<classpath>
			<pathelement location="${build.dir}/ant-contrib-0.6.jar"/>
		</classpath>
	</taskdef>
	
	<target name="all" depends="whitespace, clean, concatenate, copy-all, minify, replace-paths, wgz" description="Release builder"></target>

	<target name="deploy" depends="whitespace, clean, concatenate, minify, copy"></target>

	<target description="Zip the package" name="wgz">

		<echo message="Zipping files."/>

		<exec executable="zip">
			<arg line="-r"/>
			<arg line="${dist.dir}/../${release.filename}.wgz"/>
			<arg line="${dist.dir}"/>
		</exec>
		
	</target>

	<target name="concatenate">
		<echo message="Building concatenated"/>
		<mkdir dir="${dist.dir}/lib/"/>
		<mkdir dir="${dist.dir}/lib/jquery/"/>
		
		<delete file="${dist.dir}/lib/${concatenated}.js"/>

		<concat destfile="${dist.dir}/lib/${concatenated}.js">
			<filelist dir="${src.dir}/" files="${core.files}"/>
		</concat>
		
		<delete file="${dist.dir}/lib/${concatenated-lib}.js"/>

		<concat destfile="${dist.dir}/lib/jquery/${concatenated-lib}.js">
			<filelist dir="${lib.dir}/jquery/" files="${lib.files}"/>
		</concat>
		
		<echo message="Concatenated built."/>

		<concat destfile="${dist.dir}/${themeroller.theme.dir}/${concatenated-themeroller}.css">
			<fileset dir="${base.dir}/${themeroller.theme.dir}/" includes="ui.core.css"/>
			<fileset dir="${base.dir}/${themeroller.theme.dir}/" includes="ui.*.css" excludes="ui.all.css, ui.core.css, ui.base.cs, ui.theme.css"/>
			<fileset dir="${base.dir}/${themeroller.theme.dir}/" includes="ui.theme.css"/>
		</concat>

		<echo message="Concatenated theme."/>

		<mkdir dir="${dist.dir}/lib/i18n/"/>
		<delete file="${dist.dir}/lib/i18n/${concatenated}.i18n.js"/>

		<concat destfile="${dist.dir}/lib/i18n/${concatenated}.i18n.js" encoding="utf-8">
			<fileset dir="${src.dir}/i18n/" includes="*.js"/>
		</concat>
		<echo message="Concatenated i18n built."/>
	</target>

	<target name="minify" depends="concatenate" description="Remove all comments and whitespace, no compression, great in combination with GZip">
		<echo message="Building minified"/>
		<delete file="${dist.dir}/lib/${concatenated}.min.js"/>
		<delete file="${dist.dir}/lib/jquery/${concatenated-lib}.min.js"/>
		<delete file="${dist.dir}/lib/i18n/${concatenated}.i18n.min.js"/>
		
		<if>
			<equals arg1="${debug}" arg2="off" />
			<then>
		
				<if>
					<equals arg1="${minifier}" arg2="yui" />
					<then>
						<echo message="Minifier: YUI Compressor" />
						
						<apply executable="java" parallel="false">
							<fileset dir="${dist.dir}/lib/" includes="*.js"/>
							<arg line="-jar"/>
							<arg path="${yui-jar}"/>
							<arg value="--charset"/>
							<arg value="UTF-8"/>
							<srcfile/>
							<arg line="-o"/>
							<mapper type="glob" from="*.js" to="${dist.dir}/lib/*.min.js"/>
							<targetfile/>
						</apply>
						
					 </then>
				</if>
				
				<if>
					<equals arg1="${minifier}" arg2="google" />
					<then>
						<echo message="Minifier: Google Compiler" />
							
						<apply executable="java" parallel="false">
							<fileset dir="${dist.dir}/lib/" includes="*.js"/>
							<arg line="-jar"/>
							<arg path="${closure-compiler-jar}"/>
							<arg line="--compilation_level" />
							<arg value="SIMPLE_OPTIMIZATIONS"/>
							<arg line="--js"/>
							<srcfile/>
							<arg line="--js_output_file"/>
							<mapper type="glob" from="*.js" to="${dist.dir}/lib/*.min.js"/>
							<targetfile/>
						</apply>
					</then>			
				</if>
		
				<delete>
					<fileset dir="${dist.dir}/lib/" includes="*.js" excludes="*.min.js" />
				</delete>
				
				<move overwrite="true" todir="${dist.dir}/lib/" includeemptydirs="false">
					<fileset dir="${dist.dir}/lib/"></fileset>
					<mapper type="glob" from="*.min.js" to="*.js"/>
				</move>
				
			</then>
		</if>
		
		<apply executable="java" parallel="false">
			<filelist dir="${dist.dir}/lib/jquery/" files="${concatenated-lib}.js"/>
			<arg line="-jar"/>
			<arg path="${yui-jar}"/>
			<arg value="--charset"/>
			<arg value="UTF-8"/>
			<srcfile/>
			<arg line="-o"/>
			<mapper type="glob" from="*.js" to="${dist.dir}/lib/jquery/*.min.js"/>
			<targetfile/>
		</apply>
		
		<delete>
			<fileset dir="${dist.dir}/lib/jquery/" includes="*.js" excludes="*.min.js" />
		</delete>
		
		<move overwrite="true" todir="${dist.dir}/lib/jquery/" includeemptydirs="false">
			<fileset dir="${dist.dir}/lib/jquery/"></fileset>
			<mapper type="glob" from="*.min.js" to="*.js"/>
		</move>
		
		<echo message="Minified built."/>

		<apply executable="java" parallel="false">
			<filelist dir="${dist.dir}/lib/i18n/" files="${concatenated}.i18n.js"/>
			<arg line="-jar"/>
			<arg path="${yui-jar}"/>
			<arg value="--charset"/>
			<arg value="UTF-8"/>
			<srcfile/>
			<arg line="-o"/>
			<mapper type="glob" from="*.js" to="${dist.dir}/lib/i18n/*.min.js"/>
			<targetfile/>
		</apply>
		
		<delete>
			<fileset dir="${dist.dir}/lib/i18n/" includes="*.js" excludes="*.min.js" />
		</delete>
		
		<move overwrite="true" todir="${dist.dir}/lib/i18n/" includeemptydirs="false">
			<fileset dir="${dist.dir}/lib/i18n/"></fileset>
			<mapper type="glob" from="*.min.js" to="*.js"/>
		</move>
		
		<echo message="Minified i18n built."/>
	</target>

	<target description="Copy needed folders" name="copy-all">
		<echo message="Copying files"/>
		<mkdir dir="${dist.dir}"/>
		
		<copy overwrite="true" todir="${dist.dir}/${nokia.theme.dir}/ext-theme/${theme}/${resolution}/">
			<fileset dir="${base.dir}/${nokia.theme.dir}/ext-theme/${theme}/${resolution}/"/>
		</copy>

		<copy overwrite="true" todir="${dist.dir}/demos/">
			<fileset dir="${base.dir}/demos/${demofolder}/" includes="**/"/>
		</copy>

		<copy overwrite="true" todir="${dist.dir}/${themeroller.theme.dir}/images/">
			<fileset dir="${base.dir}/${themeroller.theme.dir}/images/"/>
		</copy>

		<copy overwrite="true" todir="${dist.dir}/${nokia.theme.dir}/base/images/">
			<fileset dir="${base.dir}/${nokia.theme.dir}/base/images/"/>
		</copy>

		<copy overwrite="true" todir="${dist.dir}/test/">
			<fileset dir="${base.dir}/test/"/>
		</copy>

		<copy overwrite="true" todir="${dist.dir}/">
			<filelist dir="${base.dir}/" files="Info.plist, config.xml, icon.png"/>
		</copy>
		
		<java jar="${build.dir}/js.jar" fork="true">
            <arg value="${build.dir}/js/copydependencies.js" />
            <arg value="${components}" />
        </java>
        
        <replaceregexp match="window\.NOKIA_PATH_BASE = '\.\.\/';" replace="window.NOKIA_PATH_BASE = '../../';" flags="g" byline="true">
			<fileset dir="${dist.dir}/lib/" includes="Guarana.js"/>
		</replaceregexp>
		
		<replaceregexp match="window\.NOKIA_PATH_JAVASCRIPT = NOKIA_PATH_BASE \+ 'src\/';" replace="window.NOKIA_PATH_JAVASCRIPT = NOKIA_PATH_BASE + 'lib/';" flags="g" byline="true">
			<fileset dir="${dist.dir}/lib/" includes="Guarana.js"/>
		</replaceregexp>

		<echo message="Files copied."/>
	</target>

	<target description="Copy needed folders" name="copy">
		<echo message="Copying files"/>
		<mkdir dir="${dist.dir}"/>

		<copy overwrite="true" todir="${dist.dir}/${themeroller.theme.dir}/images/">
			<fileset dir="${base.dir}/${themeroller.theme.dir}/images/"/>
		</copy>

		<copy overwrite="true" todir="${dist.dir}/${nokia.theme.dir}/base/images/">
			<fileset dir="${base.dir}/${nokia.theme.dir}/base/images/"/>
		</copy>
	</target>

	<target name="clean">
		<delete dir="${dist.dir}"/>
		<delete file="${dist.dir}/../${release.filename}.wgz"/>
	</target>

	<target name="whitespace">
		<replaceregexp match="[\t ]+$" replace="" flags="g" byline="true">
			<fileset dir="${src.dir}" includes="*.js"/>
			<fileset dir="${src.dir}/i18n/" includes="*.js"/>
		</replaceregexp>
		<echo message="All trailing spaces removed."/>
	</target>

	<target name="replace-paths">		
		<replaceregexp match="^\s*\u003C(link rel=\u0022stylesheet\u0022 href=|script src=)[^\u003E]+\u003E(\u003C[/][^\u003E]+\u003E)?" replace="" flags="g" byline="true">
			<fileset dir="${dist.dir}/demos/" includes="**/*.html*/"/>
			<fileset dir="${dist.dir}/test/" includes="*.html"/>
		</replaceregexp>
		
		<replaceregexp match="^\s*\u0024\('#switcher'\)\.themeswitcher\(\)\;" replace="" flags="g" byline="true">
			<fileset dir="${dist.dir}/demos/" includes="**/*.html*/"/>
			<fileset dir="${dist.dir}/test/" includes="*.html"/>
		</replaceregexp>

		<replaceregexp match="^\s*console\.log\([^\)]+\);" replace="" flags="g" byline="true">
			<fileset dir="${dist.dir}/demos/" includes="**/*.html*/"/>
		</replaceregexp>

		<replaceregexp match="^\s*(\u003C)head[^\u003E]*(\u003E)" replace="\1head\2 \1!-- auto --\2 \1link rel='stylesheet' href='../../themes/themeroller/${theme}-theme/${concatenated-themeroller}.css' type='text/css' media='screen'\2 \1link rel='stylesheet' href='../../themes/nokia/ext-theme/${theme}/${resolution}/custom.css' type='text/css' media='screen'\2 \1script src='../../lib/jquery/jquery.js' type='text/javascript' charset='utf-8'\2\1/script\2 \1script src='../../lib/Guarana.js' type='text/javascript' charset='utf-8'\2\1/script\2 \1script type='text/javascript'\2 NOKIA_PATH_JAVASCRIPT = '../../lib/'; NOKIA_PATH_STYLE_ROOT = '../../themes/nokia/base/'; \1/script\2 \1!-- auto --\2" flags="g" byline="true">
			<fileset dir="${dist.dir}/demos/" includes="**/*.html*/"/>
		</replaceregexp>

		<replaceregexp match="^\s*(\u003C)head[^\u003E]*(\u003E)" replace="\1head\2 \1!-- auto --\2 \1link rel='stylesheet' href='../themes/themeroller/${theme}-theme/${concatenated-themeroller}.css' type='text/css' media='screen'\2 \1link rel='stylesheet' href='../themes/nokia/ext-theme/${theme}/${resolution}/custom.css' type='text/css' media='screen'\2 \1script src='../lib/jquery/jquery.js' type='text/javascript' charset='utf-8'\2\1/script\2 \1script src='../lib/Guarana.js' type='text/javascript' charset='utf-8'\2\1/script\2 \1script type='text/javascript'\2 NOKIA_PATH_JAVASCRIPT = '../lib/'; NOKIA_PATH_STYLE_ROOT = '../themes/nokia/base/'; \1/script\2 \1!-- auto --\2" flags="g" byline="true">
			<fileset dir="${dist.dir}/test/" includes="*.html"/>
		</replaceregexp>

		<replaceregexp match="demos/${demofolder}(\\|\/)[^\u003C]+" replace="demos/${demofolder}\\\\${plist.component}\\\\sample_${plist.component}.html" flags="g" byline="true">
			<fileset dir="${dist.dir}/" includes="Info.plist"/>
		</replaceregexp>

		<replaceregexp match="(\n\n\n|\r\n\r\n\r\n)+" replace="" flags="g">
			<fileset dir="${dist.dir}/demos/" includes="*/*.html"/>
			<fileset dir="${dist.dir}/test/" includes="*/*.html"/>
		</replaceregexp>
		
		<echo message="Demos paths fixed."/>
	</target>

</project>